<?php

namespace app\api\model;


class CategoryModel extends BaseModel
{
    protected $pk = 'id';
    protected $table = 'hs_category';
    protected static $tableName = 'category';

    //类型常量
    const TYPE_RECYCLE = 'recycle'; //回收
    const TYPE_HOME = 'home'; //家政

    //状态常量
    const STATUS_OPEN = 1;//启用
    const STATUS_CLOSE = 0;//禁用

    //热销常量
    const IS_HOT = 1;//热销
    const NO_HOT = 0;//不热销

    public function goods()
    {
        return $this->hasMany(GoodsModel::class, 'category_id', 'id')->field('id,category_id,title,image,price,unit,remark');
    }

    public function getImageAttr($value)
    {
        if ($value) {
            $host = $_SERVER['HTTP_HOST'];
            return 'https://' . $host . $value;
        }
        return '';
    }

    public static function typeList($type, $with = '')
    {
        $list = self::where('status', self::STATUS_OPEN)->where('type', $type);
        if ($with) {
            $list = $list->with($with);
        }
        $list = $list->field('id,title,image,keywords,jump_url')->order('weigh desc')->select();
        return $list;
    }

    public static function typeArr($type)
    {
        return self::where('status', self::STATUS_OPEN)->where('type', $type)->column('title', 'id');
    }

    public static function hotList($limit = 0)
    {
        $list = self::where('status', self::STATUS_OPEN)->where('is_hot', self::IS_HOT)->where('type',self::TYPE_HOME)->field('id,title,image,keywords,jump_url')->order('weigh desc');
        if ($limit) {
            $list = $list->limit($limit);
        }
        $list = $list->select();
        return $list;
    }

}